package com.future.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Description:
 *
 * @author weiruibai.vendor
 * Date: 2022/9/14 22:47
 */
@RestController
public class OrderController {


    private static final Logger LOGGER = LoggerFactory.getLogger(OrderController.class);

    private static final String URL = "http://SELL-TICKET";

    private AtomicInteger SUM = new AtomicInteger(0);

    @Autowired
    private RestTemplate restTemplate;

    @PostMapping("/order")
    @ResponseBody
    public String order() {
        LOGGER.info("请求次数数：{}", SUM.incrementAndGet());
        return restTemplate.postForObject(URL + "/order", null, String.class);
    }

    @PostMapping("/order_v2")
    @ResponseBody
    public String order_v2() {
        LOGGER.info("请求次数数：{}", SUM.incrementAndGet());
        return restTemplate.postForObject(URL + "/order_v2", null, String.class);
    }

    @PostMapping("/order_v3")
    @ResponseBody
    public String order_v3() {
        LOGGER.info("请求次数数：{}", SUM.incrementAndGet());
        return restTemplate.postForObject(URL + "/order_v3", null, String.class);
    }

    @PostMapping("/order_zk")
    @ResponseBody
    public String order_zk() {
        return restTemplate.postForObject(URL + "/order_zk", null, String.class);
    }

    @PostMapping("/order_zk2")
    public String order_zk(@RequestParam("id") Integer id) {
        return restTemplate.postForObject(URL + "/order_zk", Integer.class, String.class);
    }

    @GetMapping("/getPort")
    public String getEnvironment() {
        return restTemplate.getForObject(URL + "/getPort", String.class);
    }

    @GetMapping("/clear")
    public String clearCount() {
        SUM.set(0);
        LOGGER.info("clear OK");
        return "clear OK";
    }
}
